﻿function TweetAway() {
    var currentDate = new Date();
    var sendMessage = JSON.stringify({
        Message: "Message 1",
        Username: "<%=GetCurrentUserName()%>"
    });

    $.ajaxSetup({
        url: "TwitterProxy.aspx",
        type: "POST",
        async: true,
        global: true,
        timeout: 3600000,
        contentType: "application/json",
        dataType: "json"
    });

    $.ajax({
        success: function (msg) {
            //alert("ajax.success().");
            var serverMsg = JSON.stringify(msg);
            //alert(msg + " string: " + serverMsg);

            if (msg == null || msg.Message == null) {
                //ChatAway();
                return;
            }

            var messageStr = String(msg.Message);
            var usernameStr = String(msg.Username);

            //if (messageStr.startsWith("[Login]")) {
            //    // Update the user list by adding this username:
            //    alert("Message received from Comet Server");

            //    ChatAway(1);
            //    return;
            //   if (msg.Type == 0) {
            //   $("#lstUserList").append(msg.Username);
            //       alert(msg.Username + " has joined the chat.");
            //       ChatAway();
            //       return;
            //    }
            //    else {
            //alert("Message received from Comet Server");
            //$("#txtMessageList").append(msg.Username + ": " + msg.Message + "<br/>");
            //$("#txtMessageList").innerhtml = msg.Username + ": " + msg.Message + "<br/>" + $("txtMessageList").html;
            //alert("Message received from server: " + msg);


            // Derrick, the next commented out line was before
            //document.getElementById("txtMessageList").innerHTML += "<br/>" + usernameStr + ": " + messageStr;
            //document.getElementById("txtMessageList").innerHTML += "<span class='userN'>" + usernameStr + ":</span><br/><span class='messageBody'>" + messageStr + "</span><br/>";
            var messageToAppend = "<span class='userN'>" + usernameStr + ":</span><br/><span class='messageBody'>" + messageStr + "</span><br/>";
            $("#_cometChatMessages").append("<li>" + messageToAppend + "</li>");
            if (usernameStr == "<%=CEOName%>") {
                document.getElementById("ceoMessage").innerHTML = "";
            }

            ChatAway();
            $("#txtMessageList").scrollTop($("#txtMessageList").get(0).scrollHeight);
            return;
            //  }
        },

        error: function () {
            TweetAway();
        },

        complete: function () {
            TweetAway();
        }
    });
}

